package com.tandong.multidatasource.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.tandong.multidatasource.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1Config extends HikariConfig {

    @Primary
    @Bean(name = "db1")
    @ConfigurationProperties(prefix = "datasource.db1")   //数据源自动配置
    public DataSource db1ataSource() {
        return new AtomikosDataSourceBean();
    }

    @Bean(name = "db1SqlSessionFactory")
    public SqlSessionFactory db1SessionFactory(@Qualifier("db1") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
//        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//        configuration.setLogImpl(StdOutImpl.class);//打印mybatis日志
        return bean.getObject();
    }

    @Bean(name = "db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

//    public DataSource db1ataSource(DB1Config config) throws SQLException {
//        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
//        mysqlXaDataSource.setUrl(config.getUrl());
//        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//        mysqlXaDataSource.setPassword(config.getPassword());
//        mysqlXaDataSource.setUser(config.getUsername());
//        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//
//        // 将本地事务注册到创 Atomikos全局事务
//        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
//        xaDataSource.setXaDataSource(mysqlXaDataSource);
//        xaDataSource.setUniqueResourceName("db1");
//
//        xaDataSource.setMinPoolSize(config.getMinPoolSize());
//        xaDataSource.setMaxPoolSize(config.getMaxPoolSize());
//        xaDataSource.setMaxLifetime(config.getMaxLifetime());
//        xaDataSource.setBorrowConnectionTimeout(config.getBorrowConnectionTimeout());
//        xaDataSource.setLoginTimeout(config.getLoginTimeout());
//        xaDataSource.setMaintenanceInterval(config.getMaintenanceInterval());
//        xaDataSource.setMaxIdleTime(config.getMaxIdleTime());
//        xaDataSource.setTestQuery(config.getTestQuery());
//        return xaDataSource;
//    }
}
